package com.itqdkj.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itqdkj.health.constant.MessageConstant;
import com.itqdkj.health.utils.POIUtils;
import com.itqdkj.health.entity.Result;
import com.itqdkj.health.pojo.OrderSetting;
import com.itqdkj.health.service.OrderSettingService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/ordersetting")
public class OrderSettingController {
    @Reference
    OrderSettingService orderSettingService;

    /**
     * 1.通过POI解析文件，得到List<String[]> list
     * 2.把List（String[]) list转成List(OrderSetting) list
     * 3.调用业务，进行保存
     * @param excelFile
     * @return
     */
    @RequestMapping(value = "/upload")
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile){
        try {
            //读取Excel文件数据
            List<String[]> list = POIUtils.readExcel(excelFile);
             if(list != null && list.size()>0){
                 List<OrderSetting> orderSettingList = new ArrayList<OrderSetting>();
                 for (String[] strings : list) {
                     OrderSetting orderSetting = new OrderSetting(new Date(strings[0]),Integer.parseInt(strings[1]));
                     orderSettingList.add(orderSetting);
                 }

                 orderSettingService.add(orderSettingList);
             }
             return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }
    @RequestMapping(value = "/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 点击设置按钮，完成针对当前日期更新预约人数的数量
     * @param orderSetting
     * @return
     */
    @RequestMapping(value = "/setOrderSetting")
    public Result setOrderSetting(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.setOrderSetting(orderSetting);
            return  new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
